﻿using Mapster;
using Nantianmen.Domain.Repositories;

namespace Nantianmen.Domain.Services.Destination
{
    public class DestinationService : DestinationQueryService, IDestinationService
    {
        public DestinationService(IDestinationStore destinationRepository) : base(destinationRepository)
        {
        }

        public async Task CreateAsync(SaveDestinationRequest request)
        {
            var model = request.Adapt<Models.Destination>();
            await this.DestinationRepository.InsertAsync(model, CancellationToken);
        }

        public async Task UpdateAsync(SaveDestinationRequest request)
        {
            var current = await this.DestinationRepository.FindByIdAsync(request.Address, CancellationToken);
            if (current is null)
            {
                throw new InvalidOperationException($"无法更新，未找到：{request.Address}");
            }
            current.Remark = request.Remark;
            current.Host = request.Host;
            current.Health = request.Health;

            await this.DestinationRepository.UpdateAsync(current, CancellationToken);
        }

        public async Task RemoveAsync(RemoveDestinationRequest request)
        {
            await this.DestinationRepository.RemoveByIdAsync(request.Id, CancellationToken);
        }

        public async Task<List<Models.Destination>> LoadAsync(List<string> destinationIds)
        {
            return await this.DestinationRepository.LoadAsync(destinationIds, CancellationToken);
        }
    }
}
